<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title>管理定区/调度排班</title>
		<!-- 导入jquery核心类库 -->
		<script type="text/javascript" src="../../js/jquery-1.8.3.js"></script>
		<!-- 导入easyui类库 -->
		<link rel="stylesheet" type="text/css" href="../../js/easyui/themes/default/easyui.css">
		<link rel="stylesheet" type="text/css" href="../../js/easyui/themes/icon.css">
		<link rel="stylesheet" type="text/css" href="../../js/easyui/ext/portal.css">
		<link rel="stylesheet" type="text/css" href="../../css/default.css">
		<script type="text/javascript" src="../../js/easyui/jquery.easyui.min.js"></script>
		<script type="text/javascript" src="../../js/easyui/ext/jquery.portal.js"></script>
		<script type="text/javascript" src="../../js/easyui/ext/jquery.cookie.js"></script>
		<script src="../../js/easyui/locale/easyui-lang-zh_CN.js" type="text/javascript"></script>
		<script type="text/javascript">

			//序列化条件变成json
            $.fn.serializeJson=function(){
                var serializeObj={};
                var array=this.serializeArray();
                var str=this.serialize();
                $(array).each(function(){
                    if(serializeObj[this.name]){
                        if($.isArray(serializeObj[this.name])){
                            serializeObj[this.name].push(this.value);
                        }else{
                            serializeObj[this.name]=[serializeObj[this.name],this.value];
                        }
                    }else{
                        serializeObj[this.name]=this.value;
                    }
                });
                return serializeObj;
            };


            function doAdd(){
				$('#addWindow').window("open");
			}
			
			function doEdit(){
				var rows = $("#grid").datagrid("getSelections");
				//alert(rows[0].id);
                if (rows.length == 1) {
                    //只选择了一行
                    $("#fixedAreaForm").form("load",rows[0]);
                    $("#addWindow").window("open");
                }else if (rows.length ==0){
                    $.messager.alert("警告","请至少选择一项,(#^.^#)","warning");
                } else {
                    $.messager.alert("警告","请不要多选,(#^.^#)","warning");

                }
			}
			
			function doDelete(){
				alert("删除...");
			}
			
			function doSearch(){
				$('#searchWindow').window("open");
			}

			function doCourier() {
                //先判断表格是否符合要求
                var rows = $("#grid").datagrid("getSelections");
                if (rows.length == 1) {
                    $('#courierWindow').window('open');
                    //设置id
                    $("#customerFixedAreaId").val(rows[0].id);


                }else if (rows.length ==0){
                    $.messager.alert("警告","请至少选择一个分区进行关联...","warning");
                } else{
                    $.messager.alert("警告","请不要多选分区...(*￣︶￣*)","warning");

                }

            }
			
			function doAssociations(){
                //点击关联客户进行校验,数据表格中选项是否符合要求
                var rows = $("#grid").datagrid("getSelections");
                if (rows.length == 1) {
                    //打开关联窗口

                    //将选择的id赋值给隐藏窗口,传递给后台代码
                    $("#customerFixedAreaId").val(rows[0].id);
                    //alert(rows[0].id);
                    $.messager.progress({
                        interval : 300
                    });
                    //弹出窗口后发起两个ajax请求分别查询已关联客户和未关联客户
                    // 发起Ajax请求
                    $.post("../../fixedArea_findNoAssociationCustomers.action", function(data){
                        // 查询所有 未关联定区客户列表,清空列表是在循环外边
                        $("#noassociationSelect").empty();
                        $(data).each(function(){
                            // 清空列表

                            var option = $("<option value='"+this.id+"'>"+this.username+"("+this.address+")</option>");
                            $("#noassociationSelect").append(option);
                            //页面加载完毕关闭进度条

                        });

                        // 请求结束后，关闭进度条
                        $.messager.progress('close');
                        // 弹出关联窗口
                        $('#customerWindow').window('open');
                    });
                    $.post("../../fixedArea_findHasAssociationFixedAreaCustomers.action",{"id":rows[0].id} , function(data){
                        // 查询所有 关联当前选中定区客户列表'
                        $("#associationSelect").empty();
                        $(data).each(function(){
                            // 清空列表
                            var option = $("<option value='"+this.id+"'>"+this.username+"("+this.address+")</option>");
                            $("#associationSelect").append(option);
                        });
                        //双击添加关联与未关联用户
                        /*==============================================*/
                        $("option").dblclick(function () {
                            //找到当前的父类标签的id
                            var selectId = this.parentNode.id;
                            //如果点击的是未关联的,则改成关联
                            if (selectId == "noassociationSelect") {
                                $("#associationSelect").append(this);
                            }else {
                                $("#noassociationSelect").append(this);
                            }
                        });
                        /*==============================================*/
                    });

                } else if (rows.length == 0){
                    $.messager.alert("警告","请至少选择一项...","warning");
                } else{
                    $.messager.alert("警告","请不要多选...","warning");
                }

			}
			
			//工具栏
			var toolbar = [ {
				id : 'button-search',	
				text : '查询',
				iconCls : 'icon-search',
				handler : doSearch
			}, {
				id : 'button-add',
				text : '增加',
				iconCls : 'icon-add',
				handler : doAdd
			}, {
				id : 'button-edit',	
				text : '修改',
				iconCls : 'icon-edit',
				handler : doEdit
			},{
				id : 'button-delete',
				text : '删除',
				iconCls : 'icon-cancel',
				handler : doDelete
			},{
				id : 'button-association',
				text : '关联客户',
				iconCls : 'icon-sum',
				handler : doAssociations
			},{
				id : 'button-association-courier',
				text : '关联快递员',
				iconCls : 'icon-sum',
				handler : doCourier
			}];
			// 定义列
			var columns = [ [ {
				field : 'id',
				title : '编号',
				width : 80,
				align : 'center',
				checkbox:true
			},{
				field : 'fixedAreaNum',
				title : '定区编号',
				width : 120,
				align : 'center',
				formatter : function(value,row,index){
					return row.id ;
				}
			},{
				field : 'fixedAreaName',
				title : '定区名称',
				width : 120,
				align : 'center'
			}, {
				field : 'fixedAreaLeader',
				title : '负责人',
				width : 120,
				align : 'center'
			}, {
				field : 'telephone',
				title : '联系电话',
				width : 120,
				align : 'center'
			}, {
				field : 'company',
				title : '所属公司',
				width : 120,
				align : 'center'
			} ] ];
			
			$(function(){
				// 先将body隐藏，再显示，不会出现页面刷新效果
				$("body").css({visibility:"visible"});
				
				// 定区数据表格
				$('#grid').datagrid( {
					iconCls : 'icon-forward',
					fit : true,
					border : true,
					rownumbers : true,
					striped : true,
					pageList: [30,50,100],
					pagination : true,
					toolbar : toolbar,
					url : "../../fixedArea_pageQuery.action",
					idField : 'id',
					columns : columns,
					onDblClickRow : doDblClickRow
				});
				
				// 添加、修改定区
				$('#addWindow').window({
			        title: '添加修改定区',
			        width: 600,
			        modal: true,
			        shadow: true,
			        closed: true,
			        height: 400,
			        resizable:false
			    });
				
				// 查询定区
				$('#searchWindow').window({
			        title: '查询定区',
			        width: 400,
			        modal: true,
			        shadow: true,
			        closed: true,
			        height: 400,
			        resizable:false
			    });
                //向右添加
                $("#toRight").click(function () {
                    $("#associationSelect").append($("#noassociationSelect option:selected"));
                });
                //向左添加
                $("#toLeft").click(function () {
                    $("#noassociationSelect").append($("#associationSelect option:selected"));
                });
                //关联客户的操作
                $("#associationBtn").click(function () {
                    //当点击关联客户时,左侧的未关联客户不用管,只提交右侧就行,
                    //所以我们就只给右侧的输入框一个name,右侧不给name就不会提交
                    //提交之前现将右侧所有的用户选项选中再提交
                    $("#associationSelect option").attr("selected","selected");
                    $("#customerForm").submit();
                });
                

				$("#btn").click(function(){
					//序列化customer表格
				    var params = $("#searchForm").serializeJson();
                    // 绑定参数到datagrid
					//alert(params)
                    $('#grid').datagrid("load",params);
                    // 关闭查询窗口
                    $("#searchWindow").window('close');
				});
				//添加fixedArea
				$("#save").click(function () {
				  	//对表单进行校验
                    if ($("#fixedAreaForm").form("validate")) {
                        //说明表单校验完成可以提交
						$("#fixedAreaForm").submit();
                    }else{
                        //说明表单中有未填写项.
						$.messager.alert("警告","表单中有非法数据,请检查后在提交","warning");
					}
                });

                //对表单提交添加事件
                $("#associationCourierBtn").click(function () {
                    var rows = $("#grid").datagrid("getSelections");
                    if (rows.length == 1) {
                        $("#courierWindow").window("open");
                        //对表单进行校验
                        if ($("#courierForm").form("validate")) {
                            $("#courierFixedAreaId").val(rows[0].id);
                            $("#courierForm").submit();
                        }else {
                            $.messager.alert("警告","表单内有非法数据,请校验","warning");
                        }
                    }else if (rows.length ==0){
                        $.messager.alert("警告","请至少选择一项分区,(*￣︶￣*)","warning");
                    }else{
                        $.messager.alert("警告","请不要多选,(*￣︶￣*)","warning");
                    }
                });


				
			});
		
			function doDblClickRow(){
				//alert("双击表格数据...");
				$('#association_subarea').datagrid( {
					fit : true,
					border : true,
					rownumbers : true,
					striped : true,
					url : "../../data/sub_area.json",
					columns : [ [{
						field : 'id',
						title : '分拣编号',
						width : 120,
						align : 'center'
					},{
						field : 'province',
						title : '省',
						width : 120,
						align : 'center',
						formatter : function(data,row ,index){
							if(row.area!=null){
								return row.area.province;
							}
							return "";
						}
					}, {
						field : 'city',
						title : '市',
						width : 120,
						align : 'center',
						formatter : function(data,row ,index){
							if(row.area!=null){
								return row.area.city;
							}
							return "";
						}
					}, {
						field : 'district',
						title : '区',
						width : 120,
						align : 'center',
						formatter : function(data,row ,index){
							if(row.area!=null){
								return row.area.district;
							}
							return "";
						}
					}, {
						field : 'keyWords',
						title : '关键字',
						width : 120,
						align : 'center'
					}, {
						field : 'startnum',
						title : '起始号',
						width : 100,
						align : 'center'
					}, {
						field : 'endnum',
						title : '终止号',
						width : 100,
						align : 'center'
					} , {
						field : 'single',
						title : '单双号',
						width : 100,
						align : 'center'
					} , {
						field : 'assistKeyWords',
						title : '辅助关键字',
						width : 100,
						align : 'center'
					} ] ]
				});
				$('#association_customer').datagrid( {
					fit : true,
					border : true,
					rownumbers : true,
					striped : true,
					url : "../../data/association_customer.json",
					columns : [[{
						field : 'id',
						title : '客户编号',
						width : 120,
						align : 'center'
					},{
						field : 'name',
						title : '客户名称',
						width : 120,
						align : 'center'
					}, {
						field : 'company',
						title : '所属单位',
						width : 120,
						align : 'center'
					}]]
				});
				$('#association_courier').datagrid({
					fit : true,
					border : true,
					rownumbers : true,
					striped : true,
					url : "",
					columns : [[{
						field : 'id',
						title : '编号',
						width : 120,
						align : 'center'
					},{
						field : 'courierNum',
						title : '快递员工号',
						width : 120,
						align : 'center'
					},{
						field : 'name',
						title : '快递员姓名',
						width : 120,
						align : 'center'
					}, {
						field : 'standard.name',
						title : '收派标准',
						width : 120,
						align : 'center',
						formatter : function(value,row,index){
							if(row.standard != null){
								return row.standard.name;
							}
							return null ; 
						}
					}, {
						field : 'taketime.name',
						title : '收派时间',
						width : 120,
						align : 'center',
						formatter : function(value,row,index){
							if(row.taketime != null){
								return row.taketime.name;
							}
							return null ; 
						}
					}, {
						field : 'company',
						title : '所属单位',
						width : 120,
						align : 'center'
					}]]
				});
			}
		</script>
	</head>

	<body class="easyui-layout" style="visibility:hidden;">
		<div region="center" border="false">
			<table id="grid"></table>
		</div>
		<div region="south" border="false" style="height:150px">
			<div id="tabs" fit="true" class="easyui-tabs">
				<div title="关联快递员" id="courier" style="width:100%;height:100%;overflow:hidden">
					<table id="association_courier"></table>
				</div>
				<div title="关联分区" id="subArea" style="width:100%;height:100%;overflow:hidden">
					<table id="association_subarea"></table>
				</div>
				<div title="关联客户" id="customers" style="width:100%;height:100%;overflow:hidden">
					<table id="association_customer"></table>
				</div>
			</div>
		</div>

		<!-- 添加 修改分区 -->
		<div class="easyui-window" title="定区添加修改" id="addWindow" collapsible="false" minimizable="false" maximizable="false" style="top:20px;left:200px">
			<div style="height:31px;overflow:hidden;" split="false" border="false">
				<div class="datagrid-toolbar">
					<a id="save" icon="icon-save" href="#" class="easyui-linkbutton" plain="true">保存</a>
				</div>
			</div>

			<div style="overflow:auto;padding:5px;" border="false">
				<form id="fixedAreaForm" action="../../fixedArea_save.action" method="post">
					<table class="table-edit" width="80%" align="center">
						<tr class="title">
							<td colspan="2">定区信息</td>
						</tr>
						<tr>
							<td>定区编码</td>
							<td>
								<input type="text" name="id" class="easyui-validatebox" required="true" />
							</td>
						</tr>
						<tr>
							<td>定区名称</td>
							<td>
								<input id="fixedareId" type="text" name="fixedAreaName" class="easyui-validatebox" required="true" />
							</td>
						</tr>
						<tr>
							<td>负责人</td>
							<td>
								<input name="fixedAreaLeader" class="easyui-validatebox" required="true"/>
							</td>
						</tr>
						<tr>
							<td>联系电话</td>
							<td>
								<input name="telephone" class="easyui-validatebox" required="true"/>
							</td>
						</tr>
						<tr>
							<td>所属公司</td>
							<td>
								<input name="company" class="easyui-validatebox" required="true"/>
							</td>
						</tr>
					</table>
				</form>
			</div>
		</div>
		<!-- 查询定区 -->
		<div class="easyui-window" title="查询定区窗口" id="searchWindow" collapsible="false" minimizable="false" maximizable="false" style="top:20px;left:200px">
			<div style="overflow:auto;padding:5px;" border="false">
				<form id="searchForm" >
					<table class="table-edit" width="80%" align="center">
						<tr class="title">
							<td colspan="2">查询条件</td>
						</tr>
						<tr>
							<td>定区编码</td>
							<td>
								<input type="text" name="id" class="easyui-validatebox" required="true" />
							</td>
						</tr>
						<tr>
							<td>所属单位</td>
							<td>
								<input type="text" name="company" class="easyui-validatebox" required="true" />
							</td>
						</tr>
						<tr>
							<td>分区</td>
							<td>
								<input type="text" name="subareaName" class="easyui-validatebox" required="true" />
							</td>
						</tr>
						<tr>
							<td colspan="2"><a id="btn" href="#" class="easyui-linkbutton" data-options="iconCls:'icon-search'">查询</a> </td>
						</tr>
					</table>
				</form>
			</div>
		</div>

		<!-- 关联客户窗口 -->
		<div class="easyui-window" title="关联客户窗口" id="customerWindow" modal =true  collapsible="false" closed="true"
             minimizable="false" maximizable="false" style="top:20px;left:200px;width: 600px;height: 300px;" >
			<div style="overflow:auto;padding:5px;" border="false">
				<form id="customerForm" action="../../fixedArea_associationCustomersToFixedArea.action" method="post">
					<table class="table-edit" width="80%" align="center">
						<tr class="title">
							<td colspan="3">关联客户</td>
						</tr>
						<tr>
							<td>
								<input type="hidden" name="id" id="customerFixedAreaId" />
								未关联定区客户列表 <br />
								<select id="noassociationSelect" multiple="multiple" size="10" style="width:200px;"></select>
							</td>
							<td>
								<input type="button" value="》》" id="toRight">
								<br/>
								<input type="button" value="《《" id="toLeft">
							</td>
							<td>
								已关联定区客户列表 <br />
								<select id="associationSelect" name="customerIds" multiple="multiple" size="10" style="width:200px;"></select>
							</td>
						</tr>
						<tr>
							<td colspan="3"><a id="associationBtn" href="#" class="easyui-linkbutton" data-options="iconCls:'icon-save'">关联客户</a> </td>
						</tr>
					</table>
				</form>
			</div>
		</div>
		
		<!-- 关联快递员窗口 -->
		<div class="easyui-window" title="关联快递员窗口" id="courierWindow" modal="true"
			collapsible="false" closed="true" minimizable="false" maximizable="false" style="top:20px;left:200px;width: 700px;height: 300px;">
			<div style="overflow:auto;padding:5px;" border="false">
				<form id="courierForm" 
					action="../../fixedArea_associationCourierToFixedArea.action" method="post">
					<table class="table-edit" width="80%" align="center">
						<tr class="title">
							<td colspan="2">关联快递员</td>
						</tr> 
						<tr>
							<td>选择快递员</td>
							<td>
								<!-- 存放定区编号 -->
								<input type="hidden" name="id" id="courierFixedAreaId" />
								<input type="text" name="courierId" class="easyui-combobox"
                                       data-options="url:'../../courier_findnoassociation.action',
									        valueField:'id',textField:'info'" required="true" />
							</td>
						</tr>
						<tr>
							<td>选择收派时间</td>
							<td>
								<input type="text" name="takeTimeId" class="easyui-combobox"
                                       data-options="url:'../../taketime_findAll.action',
									        valueField:'id',textField:'name'" required="true" />
							</td>
						</tr>
						<tr>
							<td colspan="3"><a id="associationCourierBtn" href="#" class="easyui-linkbutton" data-options="iconCls:'icon-save'">关联快递员</a> </td>
						</tr>
					</table>
				</form>
			</div>
		</div>
	</body>

</html>